package org.jground.rx.ui.zk.viewControl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jground.rx.IRXAccess;
import org.jground.rx.IRXObject;
import org.jground.rx.RXAccess;
import org.jground.rx.RXControl;
import org.jground.rx.RXView;
import org.jground.rx.ui.IUIAttribute;
import org.jground.rx.ui.zk.UIFrame;
import org.jground.util.L;
import org.jground.zk.zulX.HboxX;
import org.zkoss.zk.ui.Component;

public class UIAttributeTuple extends UIAttributeBase {

    private static Log logger = LogFactory.getLog(UIAttributeControlCurrency.class);

    public UIAttributeTuple createNew(UIFrame uiFrame, RXView moControl) {
        return new UIAttributeTuple(uiFrame, moControl);
    }

    public UIAttributeTuple() {}

    //

    private boolean isControl;
    // private DIV tupleDIV;
    // private String[] tupleValues;
    //
    private List<IUIAttribute<Component,UIFrame>> subAttributes = new ArrayList<IUIAttribute<Component,UIFrame>>();

    private UIAttributeTuple(UIFrame uiFrame, RXView moControl) {
        super(uiFrame, moControl);
        super.uiAttributeView = null;
        // RXTypeTuple tupleType = (RXTypeTuple)
        // moView.getMOAttribute().getMOType();
        // tupleValues =
        // tupleType.getAttRefs().toArray(StringUtils.EMPTY_STRING_ARRAY);
        isControl = moView instanceof RXControl;
        //
        // tupleDIV = new DIV("", UIStyles.GROUP2);
        IRXAccess moAccess = RXAccess.getInstance();
        HboxX hbox = new HboxX();
        for (RXView subView : moView.getAllSubViews()) {
            if (moAccess.isEntitled(moView)) {
                IUIAttribute<Component,UIFrame> uiAttribute = UIAttributeFactory.create(uiFrame, subView);
                subAttributes.add(uiAttribute);
                Component controlComponent = uiAttribute.getControlComponent();
//                if (controlComponent instanceof HtmlBasedComponent) {
//                    ((HtmlBasedComponent) controlComponent).setSclass(UIStyles.UI_CONTROL_INLINE);
//                }
                hbox.add(controlComponent);
            } else {
                L.debug(logger, "UIAttributeTuple No entitlement for ", moView);
            }
        }
        super.addToControlComponent(hbox);
        //
        // if (isControl) {
        // // ?
        // }
    }

    public void set(String... values) {
        L.debug(logger, "Set values", values);
    }

    public IRXObject updateData() {
        if (isControl) {
            for (IUIAttribute<Component,UIFrame> uiAttribute : subAttributes) {
                uiAttribute.updateData();
            }
            // super.moDataObject.set(super.moAttribute.getName(), tupleValues);
        }
        return super.moDataObject;
    }

    public void clearData() {
        super.clearData();
        for (IUIAttribute<Component,UIFrame> attribute : subAttributes) {
            attribute.clearData();
        }
    }

    public void setData(IRXObject moDataObject) {
        for (IUIAttribute<Component,UIFrame> attribute : subAttributes) {
            attribute.setData(moDataObject);
        }
        super.setData(moDataObject);
    }

}
